function out = gain_ctrl(pre, act, factor)
    err = abs(pre-act)/factor;
    if(((pre>0)&&(act>=0))||((pre>0)&&(act<=0))||((pre==0)&&(act<0)))
        t = act+err;
        if(t)>pre
            out = pre;
        else
           out = t; 
        end
    elseif(((pre<0)&&(act<=0))||((pre<0)&&(act>=0))||((pre==0)&&(act>0)))
        t = act-err;
        if(t)<pre
            out = pre;
        else
            out = t; 
        end  
    else
        out = 0;
    end
end

